# @before-stub-for-debug-begin
from python3problem541 import *
from typing import *
# @before-stub-for-debug-end

#
# @lc app=leetcode.cn id=541 lang=python3
#
# [541] 反转字符串 II
#

# @lc code=start
from typing import List


class Solution:
    def reverseStr(self, s: str, k: int) -> str:
        n = len(s)
        arr = list(s)
        idx = 0
        while idx < n:
            self.reverse(arr, idx, min(n-1, idx+k-1))
            idx = idx + 2 * k
        return ''.join(arr)
    
    def reverse(self, arr: List, start: int, end: int):
        while start < end:
            arr[start], arr[end] = arr[end], arr[start]
            start += 1
            end -= 1 
# @lc code=end

